﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace FUSE
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TitleScreen : Game
    {
        KeyboardState oldKeyboardState;
        Boolean hasSelected;
        int currentScreenSelection;
        public int ScreenSelection;
        
        Sprite background;
        Sprite startButton;
        Sprite highScoreButton;
        Sprite exitButton;
        Sprite optionsButton;

        //button textures
        Texture2D startTex;
        Texture2D highscoreTex;
        Texture2D exitTex;
        Texture2D optionsTex;
        Texture2D startHTex;
        Texture2D highscoreHTex;
        Texture2D exitHTex;
        Texture2D optionsHTex;


        //Game game;
        SpriteBatch sb;
        GraphicsDeviceManager graphics;



        public TitleScreen(GraphicsDeviceManager graphics)
        {
            // TODO: Construct any child components here
            //sb = new SpriteBatch(base.GraphicsDevice);
            //this.sb = spriteBatch; 
            this.graphics = graphics;
        }

        public void LoadContent(ContentManager contentManager)
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            background.LoadContent(contentManager, "DisplayMaybeNewMech");
            ScaleBackground(background);

            //load sprites
            startButton.LoadContent(contentManager, "startButtonTex");
            optionsButton.LoadContent(contentManager, "optionsButtonTex");
            highScoreButton.LoadContent(contentManager, "highscoreButtonTex");
            exitButton.LoadContent(contentManager, "exitButtonTex");

            //load textures
            startTex = contentManager.Load<Texture2D>("playUnhigh");
            highscoreTex = contentManager.Load<Texture2D>("hscoreUnhigh");
            exitTex = contentManager.Load<Texture2D>("exitHigh");
            optionsTex = contentManager.Load<Texture2D>("optionsUnhigh");
            startHTex = contentManager.Load<Texture2D>("playHigh");
            highscoreHTex = contentManager.Load<Texture2D>("hscoreHigh");
            exitHTex = contentManager.Load<Texture2D>("exitUnhigh");
            optionsHTex = contentManager.Load<Texture2D>("optionsButtonHighlightedTex");

            

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            // TODO: Add your initialization code here
            //sb = newSB;
            sb = new SpriteBatch(graphics.GraphicsDevice);
            //playerInput = new KeyboardState();
            hasSelected = false;         
            ScreenSelection = MyConstants.ITitleScreen; //which menu item is selected default is 0
            currentScreenSelection = MyConstants.ITitleScreen;

            background = new Sprite(graphics);
            startButton = new Sprite(graphics);
            highScoreButton = new Sprite(graphics);
            exitButton = new Sprite(graphics);
            optionsButton = new Sprite(graphics);

            //set background position
            background.SetX(0);
            background.SetY(0);
            //ScaleBackground(background);
            
                       
           
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            //Checks if user changes menu selection
            UpdateMenuSelect();

            Draw();
            
            //get old keyboardstate
            oldKeyboardState = Keyboard.GetState();
                
            base.Update(gameTime);
            
        }

        public void Draw()
        {
            //draw background
            //graphics.PreferredBackBufferWidth = background.Size.Width;
            //graphics.PreferredBackBufferHeight = background.Size.Height;
            //graphics.GraphicsDevice.PresentationParameters.BackBufferHeight = background.Size.Height;
            //graphics.GraphicsDevice.PresentationParameters.BackBufferWidth = background.Size.Width;
            
            background.Draw(sb);

            SetButtonLocation();

            //updates the selection effects
            DrawMenuButtons();
           
        }
        
        //scale the background image to the window size
        private void ScaleBackground(Sprite backgroundSprite)
        {
            Vector2 tempScale;
            //float heightRatio;
            //float widthRatio; 
            tempScale.Y = ((float)this.graphics.PreferredBackBufferHeight / (float)backgroundSprite.Size.Height);
            tempScale.X = ((float)this.graphics.PreferredBackBufferWidth / (float)backgroundSprite.Size.Width);
            //float widthRatio = (float)(800f / 600f);

            backgroundSprite.SetScale(tempScale);
            //backgroundSprite.SetScale((heightRatio + widthRatio) / 2.0f);
        }

        //updates the graphic effects associated with the menu items
        private void DrawMenuButtons()
        {
            //draw newgame button
            if (currentScreenSelection == 0)
            {
                //highlight new game button in blue
                //startButton.SetColor(Color.Blue);

                startButton.SetTexture(startHTex);
                startButton.Draw(sb);
            }
            else
            {
                startButton.SetTexture(startTex);
                startButton.Draw(sb);
            }


            //draw options button
            if (currentScreenSelection == 1)
            {
                //highlight new game button in blue
                optionsButton.SetTexture(optionsHTex);
                optionsButton.Draw(sb);
            }
            else
            {
                optionsButton.SetTexture(optionsTex);
                optionsButton.Draw(sb);
            }

            //draw tutorial button
            if (currentScreenSelection == 2)
            {
                //highlight tutorial button in blue
                highScoreButton.SetTexture(highscoreHTex);
                highScoreButton.Draw(sb);
            }
            else
            {
                highScoreButton.SetTexture(highscoreTex);
                highScoreButton.Draw(sb);
            }

            //draw load button
            if (currentScreenSelection == 3)
            {
                //highlight load button in blue
                exitButton.SetTexture(exitHTex);
                exitButton.Draw(sb);
            }
            else
            {
                exitButton.SetTexture(exitTex);
                exitButton.Draw(sb);
            }
        }
        
        //increments or decrements the menu selection based on key press
        private void UpdateMenuSelect()
        {
            if (!hasSelected)
            {
                if ( (Keyboard.GetState().IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyUp(Keys.Up)) ||
                    (Keyboard.GetState().IsKeyDown(Keys.Right) && oldKeyboardState.IsKeyUp(Keys.Right)) )
                {
                    //decrements player selection
                    if (currentScreenSelection < 3)
                    {
                        currentScreenSelection++;
                    }
                }
                else if ((Keyboard.GetState().IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyUp(Keys.Down)) ||
                    (Keyboard.GetState().IsKeyDown(Keys.Left) && oldKeyboardState.IsKeyUp(Keys.Left)))
                {
                    // 3 is the last item
                    if (currentScreenSelection > 0)
                    {
                        currentScreenSelection--;
                    }
                }
                else if ((Keyboard.GetState().IsKeyDown(Keys.Enter) && oldKeyboardState.IsKeyUp(Keys.Enter)) ||
                    (Keyboard.GetState().IsKeyDown(Keys.Space) && oldKeyboardState.IsKeyUp(Keys.Space)))
                {
                    hasSelected = true;
                }
            }
            else
            {
                // note: fix it better
                hasSelected = false;
                //screen selected update global
                ScreenSelection = currentScreenSelection;
                
            }
        }

        public void SetButtonLocation()
        {
            

            //TODO calcuate button offset for each button position
            int buttonXOffset = (this.graphics.PreferredBackBufferWidth * 1 / 6);
            int buttonYOffset = (this.graphics.PreferredBackBufferHeight * 2/3);

            //set new game button location
            startButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (startButton.Size.Width / 2)), 2)) + (buttonXOffset * 1)));
            startButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (startButton.Size.Height / 2)), 2)) + (buttonYOffset * 1.275)));

            //set option button location
            optionsButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (optionsButton.Size.Width / 2)), 2)) + (buttonXOffset * 2)));
            optionsButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (optionsButton.Size.Height / 2)), 2)) + (buttonYOffset*1.275)));

            //set tutorial button location
            highScoreButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (highScoreButton.Size.Width / 2)), 2)) + (buttonXOffset * 3)));
            highScoreButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (highScoreButton.Size.Height / 2)), 2)) + (buttonYOffset*1.265)));

            //set load button location
            exitButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (exitButton.Size.Width / 2)), 2)) + (buttonXOffset * 4)));
            exitButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (exitButton.Size.Height / 2)), 2)) + (buttonYOffset*1.275)));

        }

        public int getPlayerSelection()
        {
            return ScreenSelection;
        }
    }
}
